package eclipse.lib.graphics.primitives;
import javax.media.opengl.*;

import eclipse.Constants;
import eclipse.lib.graphics.shaders.GLColor;
import eclipse.lib.graphics.shaders.GLQuadShader;
/**
 * The primitive for a Quad.A Quad is a 4-vertex 3D object that can have a texture applied to it as well as have each individual vertex color mapped.
 */
public class GLQuad extends GLObject
{
	/**
	 * Builds a default Quad
	 */
	public GLQuad()
	{
		setPolygonCount(1);
		addPolygon(new GLPolygon(4));
		setQuad(Constants.TILERECT);
	}
	/** 
	 * Sets the Quad's shape from a list of floats
	 * @param x1 Point 0's x-coordinate
	 * @param x2 Point 0's y-coordinate
	 * @param x3 Point 0's z-coordinate
	 * @param x4 Point 1's x-coordinate
	 * @param y1 Point 1's y-coordinate
	 * @param y2 Point 1's z-coordinate
	 * @param y3 Point 2's x-coordinate
	 * @param y4 Point 2's y-coordinate
	 * @param z1 Point 2's z-coordinate
	 * @param z2 Point 3's x-coordinate
	 * @param z3 Point 3's y-coordinate
	 * @param z4 Point 3's z-coordinate
	 */
	public void setQuad
	(
		float x1, float x2, float x3, float x4,
		float y1, float y2, float y3, float y4,
		float z1, float z2, float z3, float z4
	)
	{
		GLPolygon polygon = this.getPolygon(0);
		polygon.renderState = GL.GL_QUADS;
		polygon.setPoint(0, x1, y1, z1);
		polygon.setPoint(1, x2, y2, z2);
		polygon.setPoint(2, x3, y3, z3);
		polygon.setPoint(3, x4, y4, z4);
	}	
	/**
	 * Sets the Quad's shape from an array of floats
	 * @param verts An array of floats containing the (x,y,z) coordinates of each point.
	 */
	public void setQuad(float verts[])
	{
		GLPolygon polygon = this.getPolygon(0);
		polygon.renderState = GL.GL_QUADS;
		polygon.setPoint(0, verts[0], verts[1], verts[2]);
		polygon.setPoint(1, verts[3], verts[4], verts[5]);
		polygon.setPoint(2, verts[6], verts[7], verts[8]);
		polygon.setPoint(3, verts[9], verts[10], verts[11]);
	}
	/**
	 * Sets the color of the Quad with a QuadShader
	 * @param color QuadShader used to color the quad
	 */
	public void setColor(GLQuadShader color)
	{
		GLPolygon polygon = this.getPolygon(0);
		polygon.setColor(0, color.getTopLeft());
		polygon.setColor(1, color.getTopRight());
		polygon.setColor(2, color.getBottomRight());
		polygon.setColor(3, color.getBottomLeft());
	}
	/**
	 * Sets the color of the Quad with a single color
	 * @param color Shader used to color the Quad.
	 */
	public void setColor(GLColor color)
	{
		GLPolygon polygon = this.getPolygon(0);
		for(int i =0; i < 4; i++)
			polygon.setColor(i, color);
	}
	/** 
	 * Returns the polygon representing the Quad
	 * @return A 4-point polygon of a Quad
	 */
	public GLPolygon getPolygon()
	{
		return getPolygon(0);
	}
}
